## preparation for fitting IRT model (preprocessing)
## author: Yuki Shiraito
## created on: 6/30/2014

## Data manipulation
### Delete irrelevant variables
delete.var <- c("ken", "candidate", "dist", "year", "wave",
                "party", "chamber", "voter", "cities")

if (pv == 1) {
    select.obs <- (dat.all$voter == 0)
} else if (pv == 2) {
    select.obs <- (dat.all$voter == 1)
} else {
    select.obs <- rep(TRUE, times = nrow(dat.all))
}
dat.all <- dat.all[select.obs, !(names(dat.all) %in% delete.var)]

### Missing Values
dat.all[dat.all == 99 | dat.all == 9 | dat.all == 66] <- NA

### drop NA questions
num.na <- apply(dat.all, 2, function(x) {sum(is.na(x))})
dat.all <- dat.all[, num.na != nrow(dat.all)]

## drop NA observations
num.na <- apply(dat.all, 1, function(x) {sum(is.na(x))})
dat.all <- dat.all[num.na != ncol(dat.all), ]


